﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.TeamFoundation.Client;

namespace TFSCommon.TFS
{
    public partial class TfsAddServer : Form
    {
        public String TFSServer
        {
            get { return txtServer.Text.Trim(); }
            set { txtServer.Text = value; }
        }

        public String TFSPath
        {
            get { return txtPath.Text.Trim(); }
            set { txtPath.Text = value; }
        }

        public int TFSPortNumber
        {
            get { return (int)numPortNumber.Value; }
            set { numPortNumber.Value = value; }
        }

        public String TFSProtocol
        {
            get { return cboProtocol.Text; }
            set { cboProtocol.Text = value; }
        }

        public TfsAddServer()
        {
            InitializeComponent();

            txtServer.TextChanged += new EventHandler(tfsconfig_ValueChanged);
            txtPath.TextChanged += new EventHandler(tfsconfig_ValueChanged);
            numPortNumber.ValueChanged += new EventHandler(tfsconfig_ValueChanged);
            cboProtocol.SelectedIndexChanged += new EventHandler(tfsconfig_ValueChanged);
            
            cboProtocol.SelectedIndexChanged += new EventHandler(cboProtocol_SelectedIndexChanged);

            TFSServer = string.Empty;
            TFSPath = "tfs";
            TFSProtocol = "http";
            TFSPortNumber = 8080;
        }
        
        private void tfsconfig_ValueChanged(object sender, EventArgs e)
        {
            btnOk.Enabled = !string.IsNullOrEmpty(TFSServer);

            if (string.IsNullOrEmpty(TFSServer))
            {
                txtPreview.Text = "Server name cannot be empty.";
            }
            else
            {
                if (Uri.IsWellFormedUriString(TFSServer, UriKind.Absolute))
                {
                    grpConnectionDetails.Enabled = false;
                    txtPreview.Text = TFSServer;
                }
                else
                {
                    grpConnectionDetails.Enabled = true;
                    string format = "{0}://{1}";

                    if ((TFSProtocol == "http" && TFSPortNumber != 80) ||
                        (TFSProtocol == "https" && TFSPortNumber != 443))
                    {
                        format += ":{2}";
                    }

                    format += "/{3}";

                    string URL = string.Format(format, TFSProtocol, TFSServer, TFSPortNumber, TFSPath);

                    if (Uri.IsWellFormedUriString(URL, UriKind.Absolute))
                    {
                        txtPreview.Text = URL;
                    }
                    else
                    {
                        txtPreview.Text = "Invalid server name/URL";
                    }
                }
            }
        }

        private void cboProtocol_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboProtocol.Text == "https" && TFSPortNumber == 8080)
            {
                TFSPortNumber = 443;
            }
            else if (cboProtocol.Text == "http" && TFSPortNumber == 443)
            {
                TFSPortNumber = 8080;
            }
        }

        private void TFSAddServer_HelpButtonClicked(object sender, CancelEventArgs e)
        {
            System.Diagnostics.Process.Start("http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k(VS.TFC.CONNECTTOTFSDIALOG);k(TargetFrameworkMoniker-%22.NETFRAMEWORK%2cVERSION%3dV3.5%22);k(DevLang-CSHARP)&rd=true");
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                btnOk.Enabled = false;
                TfsConfigurationServer server = new TfsConfigurationServer(new Uri(txtPreview.Text));
                server.EnsureAuthenticated();

                RegisteredTfsConnections.RegisterConfigurationServer(server);

                DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                btnOk.Enabled = true;
                Cursor = Cursors.Default;
            }
        }
    }
}
